package com.lry.pda.bottom.c.backend.executor;

import com.lry.pda.bottom.c.backend.IValueSetter;
import com.lry.pda.bottom.c.backend.code.CodeKey;
import com.lry.pda.bottom.c.backend.code.CodeNode;
import com.lry.pda.bottom.c.frontend.Grammar;

public class NoCommaExprExecutor extends BaseExecutor{
    @Override
    public void execute(CodeNode root) {
        executeChildren(root);

        int production = (int)root.getAttr(CodeKey.PRODUCTION);
        Object value;
        CodeNode child;
        if(production== Grammar.Binary_TO_NoCommaExpr){
            child = root.getChildren().get(0);
            copyChild(root, child);
        }
        else if(production==Grammar.NoCommaExpr_Equal_NoCommaExpr_TO_NoCommaExpr){
            child = root.getChildren().get(0);
            String t = (String)child.getAttr(CodeKey.TEXT);
            IValueSetter setter;;
            setter = (IValueSetter)child.getAttr(CodeKey.SYMBOL);
            child = root.getChildren().get(1);
            value = child.getAttr(CodeKey.VALUE);
            try {
                setter.setValue(value);
            } catch (Exception e) {
                e.printStackTrace();
                System.err.println("Runtime Error: Assign Value Error");
            }

            child = root.getChildren().get(0);
            child.setAttr(CodeKey.VALUE, value);
            copyChild(root, root.getChildren().get(0));
        }
    }
}
